﻿using System;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using ControleRepresentante.Common;
using WebSite.ViewModels;

namespace WebSite.Views
{
    public static class HtmlHelperExtensions
    {
        public static MvcHtmlString ShowIfNotNullOrEmpty<TValue>(this HtmlHelper helper, string caption, TValue value)
        {
            string stringValue = value == null || String.IsNullOrEmpty(value.ToString().Trim())
                ? "-- Em Branco --"
                : value.FormatForDisplay();
            return helper.FieldFor(caption, MvcHtmlString.Create(stringValue.Replace("\n", "<br/>")));
        }

        public static MvcHtmlString FieldFor(this HtmlHelper helper, string caption, params IHtmlString[] values)
        {
            StringBuilder stringBuilder = new StringBuilder();
            foreach (IHtmlString value in values)
            {
                stringBuilder.Append(value);
            }

            return
                MvcHtmlString.Create(
                    String.Format("<div class='editor-label'>{0}</div><div class='editor-field'>{1}</div>",
                                  caption,
                                  stringBuilder));
        }

        public static MvcHtmlString TextArea(this HtmlHelper helper, string caption, string name, string value, object htmlAttributes)
        {
            return helper.FieldFor(caption,
                helper.TextArea(name, value, htmlAttributes),
                helper.ValidationMessage(name));
        }

        public static ButtonsViewModel Buttons(this HtmlHelper htmlHelper)
        {
            return new ButtonsViewModel(htmlHelper);
        }

        public static FormLayoutViewModel FormLayoutViewModel(this HtmlHelper htmlHelper)
        {
            return new FormLayoutViewModel(htmlHelper);
        }
    }
}